package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.common.UserHolder;
import com.itheima.ydd.dto.PageDto;
import com.itheima.ydd.entity.YddArticle;
import com.itheima.ydd.entity.YddUser;
import com.itheima.ydd.entity.YddUserData;
import com.itheima.ydd.service.IYddArticleService;
import com.itheima.ydd.service.IYddUserDataService;
import com.itheima.ydd.service.IYddUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private IYddUserService yddUserService;

    @Autowired
    private IYddUserDataService yddUserDataService;

    @Autowired
    private IYddArticleService yddArticleService;

    /**
     * 获取当前登录对象信息
     *
     * @return
     */
    @GetMapping("/getUserInfo")
    public R getUserInfo() {

        LambdaQueryWrapper<YddUser> yddUserLQW=new LambdaQueryWrapper<>();
        yddUserLQW.eq(YddUser::getId, UserHolder.getUserId());
        YddUser one=yddUserService.getOne(yddUserLQW);
        R r=new R();
        //用户信息
        r.putData("userInfo", one);
        LambdaQueryWrapper<YddArticle> yddArticleLambdaQueryWrapper=new LambdaQueryWrapper<>();
        yddArticleLambdaQueryWrapper.eq(YddArticle::getUserId, UserHolder.getUserId());
        //文章数量
        LambdaQueryWrapper<YddArticle> yddArticleLqw=new LambdaQueryWrapper<>();
        yddArticleLqw.eq(YddArticle::getUserId, UserHolder.getUserId());
        r.putData("articleNum", yddArticleService.count(yddArticleLqw));
        //点赞数量
        LambdaQueryWrapper<YddUserData> yddUserDataQueryWrapper=new LambdaQueryWrapper<>();
        yddUserDataQueryWrapper.eq(YddUserData::getUserId, UserHolder.getUserId())
                .eq(YddUserData::getType, 2);//1代表收藏 2代表点赞
        r.putData("zanNum", yddUserDataService.count(yddUserDataQueryWrapper));
        //收藏数量
        //清空条件构造器 重新给条件
        yddUserDataQueryWrapper.clear();
        yddUserDataQueryWrapper.eq(YddUserData::getUserId, UserHolder.getUserId())
                .eq(YddUserData::getType, 1);
        r.putData("collectNum", yddUserDataService.count(yddUserDataQueryWrapper));
        r.setErrno(0);
        return r;
    }

    //userDataList?page=1&limit=10&type=1
    @GetMapping("userDataList")
    public R userDataList(Integer page, Integer limit, String type) {
        //分页数据
        Page<YddArticle> articlePage=new Page<>(page, limit);

        LambdaQueryWrapper<YddUserData> yddUserDataLQW=new LambdaQueryWrapper<>();
        yddUserDataLQW.eq(YddUserData::getUserId, UserHolder.getUserId())
                .eq(YddUserData::getType, type);
        //根据用户ID和类型(收藏/点赞) 来获取 对应的文章Id列表
        List<YddUserData> yddUserDataList=yddUserDataService.list(yddUserDataLQW);
        //初始化文章ID列表
        List<Integer> articleIds=new ArrayList<>();
        for (YddUserData yddUserData : yddUserDataList) {
            //将获取的文章ID添加到列表中
            articleIds.add(yddUserData.getArticleId());
        }
        LambdaQueryWrapper<YddArticle> yddArticleLQW=new LambdaQueryWrapper<>();
        //设置根据文章ID 获取 文章列表
        yddArticleLQW.in(YddArticle::getId, articleIds);
        //根据ID列表查询 对应文章列表
        yddArticleService.page(articlePage, yddArticleLQW);

        //造数据
        R r=new R();
        PageDto pageListDto=PageDto.getPageListDto(articlePage);

        return R.success("list", pageListDto);

    }

    //myArticle?page=1&limit=10
    @GetMapping("myArticle")
    public ResultData myArticle(Integer page, Integer limit) {
        Page<YddArticle> articlePage=new Page<>(page, limit);
        LambdaQueryWrapper<YddArticle> yddArticleLQW=new LambdaQueryWrapper<>();
        yddArticleLQW.eq(YddArticle::getUserId, UserHolder.getUserId())
                .orderByDesc(YddArticle::getUpdateDate);
        yddArticleService.page(articlePage, yddArticleLQW);
        //将分页数据封装返回
        return ResultData.ok("", PageDto.getPageListDto(articlePage));
    }

    //update
    @PostMapping("update")
    public ResultData update(@RequestBody YddUser user) {

        yddUserService.updateById(user);
        return ResultData.ok("修改成功",user);
    }

    //http://localhost/api/user/userDataHandle?type=1&action=del&    article_id=298
    @GetMapping("/userDataHandle")
    public ResultData userDataHandle(YddUserData yddUserData, String action, Integer article_id) {
        System.out.println(yddUserData);
        //取消点赞或收藏
        if ("del".equals(action)) {
            LambdaQueryWrapper<YddUserData> yddUserDataLQW=new LambdaQueryWrapper<>();
            yddUserDataLQW.eq(YddUserData::getUserId, UserHolder.getUserId())
                    .eq(YddUserData::getArticleId, article_id)
                    .eq(YddUserData::getType, yddUserData.getType());
            //移除数据
            yddUserDataService.remove(yddUserDataLQW);
            if (yddUserData.getType() == 2) {
                //减少该文章的点赞数量
                YddArticle article=yddArticleService.getById(article_id);
                article.setLovenum(String.valueOf(Integer.parseInt(article.getLovenum()) - 1));
                yddArticleService.updateById(article);
            }
        }

        if ("add".equals(action)) {
            //设置当前登录用户
            yddUserData.setUserId(Integer.parseInt(UserHolder.getUserId()));
            //添加信息
            yddUserData.setArticleId(article_id);
            yddUserDataService.save(yddUserData);
            //判断如果是点赞 则把文章点赞数增加
            if (yddUserData.getType() == 2) {
                //获取当前文章 增加点赞数
                YddArticle article=yddArticleService.getById(article_id);
                article.setLovenum(String.valueOf(Integer.parseInt(article.getLovenum()) + 1));
                yddArticleService.updateById(article);
            }
        }
        return ResultData.ok("", "");
    }

    @PostMapping("addArticle")
    public ResultData addArticle(@RequestBody YddArticle yddArticle) {

        //初始化点赞数量
        yddArticle.setLovenum("0");
        //设置文章的创建时间
        yddArticle.setCreateDate(System.currentTimeMillis());
        //设置文章的修改时间
        yddArticle.setUpdateDate(System.currentTimeMillis());
        //设置发布人
        yddArticle.setUserId(Integer.parseInt(UserHolder.getUserId()));
        yddArticleService.save(yddArticle);

        return ResultData.ok("添加文章成功!!!!!!", "");
    }

    //editArticle
    @PostMapping("editArticle")
    public ResultData editArticle(@RequestBody YddArticle yddArticle){
        //设置修改时间
        yddArticle.setUpdateDate(System.currentTimeMillis());
        yddArticleService.updateById(yddArticle);
        return ResultData.ok("修改文章成功!!!!!!", "");
    }

    @GetMapping("delArticle")
    public ResultData delArticle(Integer id){
        YddArticle byId=yddArticleService.getById(id);
        byId.setStatus(3);
        yddArticleService.updateById(byId);

        return ResultData.ok("删除成功!","");
    }
}
